﻿using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;

namespace StudentInfo.Areas.StudentArea.Services
{
    public class StudentService
    {
        public static Student Get(int studentId)
        {
            using (var context = new StudentInfoEntities())
            {
                return context.Students
                    .Include(x => x.Marks.Select(m => m.Exam))
                    .FirstOrDefault(s => s.Id == studentId);
            }
        }

        public static List<Student> GetAll()
        {
            using (var context = new StudentInfoEntities())
            {
                return context.Students
                    .Include(x => x.Marks.Select(m => m.Exam)).ToList();
            }
        }

        public static void Save(Student student)
        {
            using (var context = new StudentInfoEntities())
            {
                context.Students.Attach(student);
                context.Entry(student).State = EntityState.Modified;
                context.SaveChanges();
            }
        }

        public static void Create(Student student)
        {
            using (var context = new StudentInfoEntities())
            {
                context.Students.Add(student);
                context.SaveChanges();
            }
        }

        public static void Delete(Student student)
        {
            using (var context = new StudentInfoEntities())
            {
                context.Entry(student).State = EntityState.Deleted;
                context.SaveChanges();
            }
        }
    }
}